﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DVForm
{
    public partial class Form1 : Form
    {


        private GMS.Core.Config.DBInfo _db = null; 

        public Form1()
        {
            InitializeComponent();

            this.Load += Form1_Load;
        }

        void Form1_Load(object sender, EventArgs e)
        {
            var dbg = GMS.Core.Config.CachedConfigContext.Current.DBConfig;

            var obj = dbg.Id;

            this.listView1.BeginUpdate();

            for (int i = 0; i < dbg.dbs.Count; i++)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Tag = dbg.dbs[i].Id;
                lvi.Text = dbg.dbs[i].Text;
                this.listView1.Items.Add(lvi); 
            }

            this.listView1.EndUpdate();
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {   
            var item = listView1.FocusedItem;
            if (item == null)
                return;

            //MessageBox.Show(item.Tag.ToString());

            string dbId = item.Tag.ToString();

            _db = GMS.Core.Config.CachedConfigContext.Current.DBConfig.dbs.Where(q => q.Id == dbId).FirstOrDefault();

            M.Dao.Client.SchemaRepository sr = new M.Dao.Client.SchemaRepository(_db.Url, _db.Name, _db.AccessToken);
            string[] tables = sr.Tables();

            int count = tables.Length;

            this.listView2.BeginUpdate();
            this.listView2.Items.Clear();
            for (int i = 0; i < tables.Length; i++)
            {
                ListViewItem lvi = new ListViewItem();

                lvi.Text = tables[i];
                this.listView2.Items.Add(lvi);
            }
            this.listView2.EndUpdate();
        }
        
        private void listView2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (_db == null)
                return;

            var item = listView2.FocusedItem;
            if (item == null)
                return;

            string tableName = item.Text;


            FormView fv = new FormView(_db,tableName);
            fv.ShowDialog();

        }
    }
}
